#!/bin/sh

HINT="\e[32;1m"
ERROR="\e[31m"
NORMAL="\e[0m"
BLINK=""
UNBLINK=""

echo -e "${HINT}This script will help you to install subversion and bugzilla on a clean CentOS 6.2 minimal system${NORMAL}"
echo -e "Press any key to continue!\c"
read 

if [ ! -e /root/bugzilla-4.2.1.tar.gz ];then
    echo -e "${ERROR}Please put /root/bugzilla-4.2.1.tar.gz into your /root directory,byebye ${NORMAL}"
    exit 1
fi

if [ ! -e /root/bugzilla.zh-TW.4.2.1.20120428.tar.gz ];then
    echo -e "${ERROR}Please put /root/bugzilla.zh-TW.4.2.1.20120428.tar.gz into your /root directory,byebye ${NORMAL}"
    exit 1
fi

echo -e "Please insert Disc 2 of CentOS into your cdrom,and press Enter\c"
read
mkdir /media/{cdrom,CentOS}
while true;do
    mount /dev/cdrom /media/cdrom
    if  [ $? -eq 0 ]  
    then
        echo -e "${HINT}Copying Disk 2 into /media/CentOS,this may take some time...${NORMAL}"
        cp -r /media/cdrom/* /media/CentOS
        umount /media/cdrom
        break
    else
        echo -e "${ERROR}Cound not mount cdrom,please make sure your disk is in the cdrom and press enter${NORMAL}\c"
        read
    fi     
done

echo -e "${HINT}Please insert Disk 1 of CentOS now,then press Enter${NORMAL}\c"
read
while true;do
    mount /dev/cdrom /media/cdrom
    if [ $? -ne 0 ]
    then
        echo -e "${ERROR}Cound not mount cdrom,please make sure your disk is in the cdrom and press enter${NORMAL}\c"
        read
    else
        break
    fi
done

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.sav
sed -i 's/enabled=0/enabled=1/g' /etc/yum.repos.d/CentOS-Media.repo

echo -e "${HINT}Will try to install vim and check if the installation environment is OK...${NORMAL}"
yum install vim -y
if [ $? -eq 0 ];then
    echo "installation environment is OK"
else
    echo "installation is fail,and I don't know why. byebye!"
    exit 1
fi

yum install setuptool system-config-network-tui  system-config-firewall-tui wget nano ntp openssh-clients rsync -y
echo -e "${HINT}Please config the network,press Enter${NORMAL}\c"
read
system-config-network-tui
echo -e "${HINT}Will try to call eth0 up...${NORMAL}"
ifup eth0
MyAddr=`ifconfig eth0 | grep "inet addr" | awk -F: '{ print $2 }' | awk '{ print $1 }'`
if [ -z $MyAddr ]; then
    echo -e "${ERROR}Cound not bring up eth0,bye!${NORMAL}"
    exit 1
else
    echo -e "${HINT}Seem OK,your address is $MyAddr${NORMAL}"
fi

echo -e "${HINT}Will change eth0 cfg file to auto up${NORMAL}"
sed -i 's/ONBOOT=no/ONBOOT=yes/g' /etc/sysconfig/network-scripts/ifcfg-eth0
grep "ONBOOT=yes" /etc/sysconfig/network-scripts/ifcfg-eth0
if [ $? -ne 0 ]; then
    echo -e "${ERROR}dont know why but this step fail,byebye!${NORMAL}"
    exit 1
fi

echo -e "${HINT}Will install mysql now,please input mysql's root password you want${NORMAL}"
read MySqlPass
while [ -z $MySqlPass ]; do
    echo -e "${HINT}Please Enter mysql's root password${NORMAL}"
    read MySqlPass
done

yum install mysql-server -y
service mysqld start
chkconfig --level 35 mysqld on
/usr/bin/mysqladmin -u root password "$MySqlPass"

echo -e "${HINT}Will install Apache now,press Enter${NORMAL}"
read
yum  install httpd php php-common -y
service httpd start
chkconfig --level 35  httpd on

echo -e "${HINT}Please config the firewall if you want,press Enter${NORMAL}\c"
read
system-config-firewall-tui

echo -e "${HINT}Will install Subversion now,press Enter${NORMAL}\c"
read
yum install mod_dav_svn subversion -y
echo -e "${HINT}Will init your first svn repos,it will locate in /var/www/svn,give the repos name${NORMAL}"
read REPOS
while [ -z $REPOS ]; do
    echo -e "${HINT}Please Enter your first repos name:${NORMAL}"
    read REPOS
done

mkdir /var/www/svn
cd /var/www/svn/
svnadmin create $REPOS
chown -R apache.apache $REPOS
chcon -R -t httpd_sys_content_t $REPOS

grep "^<Location" /etc/httpd/conf.d/subversion.conf
if [ $? -ne 0 ];then
    echo """
<Location /svn>
   DAV svn
   SVNParentPath /var/www/svn

   # Limit write permission to list of valid users.
   # <LimitExcept GET PROPFIND OPTIONS REPORT>
      # Require SSL connection for password protection.
      # SSLRequireSSL

      AuthType Basic
      AuthName \"Authorization Realm\"
      AuthUserFile /etc/svn-auth-users
      AuthzSVNAccessFile /etc/svn-access-control
      Require valid-user
   # </LimitExcept>
</Location>
""">>/etc/httpd/conf.d/subversion.conf
fi

echo -e "${HINT}Please assign the admin passwd of your subversion account${NORMAL}"
htpasswd -c -m /etc/svn-auth-users admin

echo -e "${HINT}Please edit your svn access control file${NORMAL}\c"
echo """[groups]
admin = admin
 
[/]
* = 
@admin = rw
 
[$REPOS:/]
"""> /etc/svn-access-control
vi /etc/svn-access-control
service httpd reload

echo -e "${HINT}Init stand repos directory structure,you may need input your subversion account${NORMAL}"
mkdir -p /tmp/svn-structure-template/{trunk,branches,tags,doc,release}
svn import -m 'Initial import' /tmp/svn-structure-template/ http://localhost/svn/$REPOS/

echo -e "${HINT}Now will try to install bugzilla,press Enter${NORMAL}\c"
read
yum install perl-ExtUtils-MakeMake perl-Digest-SHA perl-TimeDate perl-DateTime perl-Template-Toolkit perl-GD perl-GDTextUtil perl-SOAP-Lite perl-Test-Taint perl-MIME-tools perl-GDGraph perl-YAML perl-CGI php-gd gd fontconfig mod_perl perl-Encode-Detect perl-HTML-Parser perl-Authen-SASL perl-LDAP perl-XML-Twig perl-MailTools perl-Test-Simple perl-CPAN gcc make mod_perl-devel -y

cd /var/www/html
tar zxvf /root/bugzilla-4.2.1.tar.gz
mv bugzilla-4.2.1 bugs
cd bugs
./checksetup.pl --check-modules
/usr/bin/perl install-module.pl --all
./checksetup.pl --check-modules
echo -e "${HINT}Confirm all modules are ok and input [yes]${NORMAL}"
read WELLDONE
while [ "$WELLDONE" != "yes" ]; do
    /usr/bin/perl install-module.pl --all
    ./checksetup.pl --check-modules
    echo -e "${HINT}Confirm all modules are ok and input [yes]${NORMAL}"
    read WELLDONE
done

echo -e "${HINT}input bugzilla's db password you want!${NORMAL}"
read BUGPASS
while [ -z $BUGPASS ]; do
    echo -e "${HINT}Please Enter bugzilla's db password${NORMAL}"
    read BUGPASS
done
./checksetup.pl
sed -i "s/db_pass = ''/db_pass = '$BUGPASS'/g" ./localconfig

grep "max_allowed_packet=4M" /etc/my.cnf
if [ $? -ne 0 ]; then
    sed -i '/user=mysql/i max_allowed_packet=4M' /etc/my.cnf
fi

service mysqld restart
echo "GRANT SELECT, INSERT, UPDATE, DELETE, INDEX, ALTER, CREATE, LOCK TABLES, CREATE TEMPORARY TABLES, DROP, REFERENCES ON bugs.* TO bugs@localhost IDENTIFIED BY '$BUGPASS'; FLUSH PRIVILEGES;" | mysql -uroot -p$MySqlPass 

./checksetup.pl

grep "/var/www/html/bugs" /etc/httpd/conf.d/perl.conf
if [ $? -ne 0 ];then
    echo """
PerlSwitches -I/var/www/html/bugs -I/var/www/html/bugs/lib -w -T
PerlConfigRequire /var/www/html/bugs/mod_perl.pl
""">>/etc/httpd/conf.d/perl.conf
fi
service httpd stop
setsebool -P httpd_can_network_connect=1
service httpd start

tar zxvf /root/bugzilla.zh-TW.4.2.1.20120428.tar.gz -C /tmp/
cp -r /tmp/bugzilla-tw/template/zh-TW /var/www/html/bugs/template/zh-CN

sed -n '113p' /var/www/html/bugs/template/zh-CN/default/admin/params/auth.html.tmpl  |grep "_"
if [ $? -ne 0 ];then
    sed -i '113 s/>"/>" _/' /var/www/html/bugs/template/zh-CN/default/admin/params/auth.html.tmpl 
fi
echo """  [% END %]
[% END %]
""">>/var/www/html/bugs/template/zh-CN/default/email/bugmail.txt.tmpl
./checksetup.pl

umount /media/cdrom

echo "Access BugZilla   : http://$MyAddr/bugs/"
echo "Access Subversion : http://$MyAddr/svn/$REPOS/"
echo -e "${BLINK}${HINT}Everything is done!${NORMAL}${UNBLINK}"
